package com.tjetc.dao.impl;

import com.tjetc.dao.CommentDao;
import com.tjetc.pojo.entity.Review;
import com.tjetc.utils.DruidUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/11 19:45
 * @Description: CommentDaoImpl
 */
public class CommentDaoImpl implements CommentDao {
    @Override
    public List<Review> getByHotelId(Long hotelId) throws Exception {
        return DruidUtils.r2.query("select * from review where hotel_id = ?", new BeanListHandler<>(Review.class, DruidUtils.rowProcessor), hotelId);
    }

    @Override
    public void insert(Review review) throws Exception{
        DruidUtils.r1.update(DruidUtils.getConnection(),"insert into review (hotel_id, guest_id, rating, comment, comment_time) VALUES " +
                "(?,?,?,?,?)",review.getHotelId(),review.getGuestId(),review.getRating(),review.getComment(),review.getCommentTime());
    }

    @Override
    public Review getByUHId(Long userId, Long hotelId) throws Exception{
        return DruidUtils.r2.query("select r.* from guest g,user u,review r where g.user_id=? and r.hotel_id = ?",new BeanHandler<>(Review.class),userId,hotelId);
    }
}
